#include "database.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>      /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
int database_load(MYDATABASE *database, char *filename)
{
	int fd = open(filename, O_RDONLY|O_CREAT, 0666);
        if(-1 == fd)
                return -1;

	while(1)
	{
		char name[20];
		char pass[20];
		if(0 == read(fd, name, 20) )
			break;
		read(fd, pass, 20);
		strcpy(database->user[database->num].name, name);
	    strcpy(database->user[database->num].pass, pass);
		database->num++;
	}

	close(fd);
}

int database_save(MYDATABASE *database, char *filename)
{
	int fd = open(filename, O_WRONLY);
	if(-1 == fd)
		return -1;

	int i = 0;
	for(;i<database->num; i++)
	{
		write(fd, database->user[i].name, 20);
		write(fd, database->user[i].pass, 20);
	}
	
	close(fd);
}
int database_show(MYDATABASE *database)
{
	int i = 0;
	for(;i<database->num; i++)
	{
		printf("%s\t|%s\n", database->user[i].name, 	database->user[i].pass);	
	}
}

int database_init(MYDATABASE *database)
{
	database->num = 0;
}

int database_insert(MYDATABASE *database, char *name, char *pass)
{
	strcpy(database->user[database->num].name, name);
	strcpy(database->user[database->num].pass, pass);
	printf("login was successful!\n");
	database_save(&db, "my.db");
	database->num++;
	return 0;
}

/*int database_delete(MYDATABASE *database, char *name, char *pass)
{
  

 int i,k,p=0;
   for(i=0;i<database->num;i++)
     
      if(0==strcmp(database->user[i].name,name))
        {
           if(0==strcmp(database->user[i].pass,pass))
                {
                for(k=i;k<database->num;k++)
                        {
                          strcpy(database->user[k].name,database->user[k+1].name);
                          strcpy(database->user[k].pass,database->user[k+1].pass);
                        }
                   database->num--;
		   p=1;
                   printf("successfuly deleted!\n");
                   break;
                }
          else
           {
		 printf("passwordold is wrong!!!\n");
                 p=1;
	   }
        }
    if(p==0)
          printf("No this person!!!\n");
     

}

int database_update(MYDATABASE *database, char *name, char *passold, char *passnew)
{
 
    int i,k=0;
   for(i=0;i<database->num;i++)
       if(0==strcmp(database->user[i].name,name))
        {
		k=1;
	   if(0==strcmp(database->user[i].pass,passold))
                {
                   strcpy(database->user[i].pass,passnew);
                   printf("modified successfully!\n");
                   break;  
                }
          else
            printf("passwordold is wrong!!!\n");
        }
       if(k==0)
          printf("No this person!!!\n");
}
*/
int database_select(MYDATABASE *database, char *name, char *pass)
{
   int i,k=0;
   for(i=0;i<database->num;i++)
       if(0==strcmp(database->user[i].name,name))
        {
		k=1;
           if(0==strcmp(database->user[i].pass,pass))
                {

		   printf("you have entered the QQ!\n");
		        return 1;
                break;
                }
          else
		  {
          printf("passwordold is wrong!!!\n");
		  
		   }
        }
       if(k==0)
	   {
          printf("No this person!!!\n");
		 
	   }

}
void xxx(char buf[100],int *a,int *b)
{   
	int h;
	h=*b;
		//printf("recv: %s\n", buf);
		//3)write();
		//write(clientfd, "ok", 2);
		//printf("DB> ");
		//fflush(stdout);
		//gets(buf);
		/*if(0 == strcmp("help", buf) )
		{
			printf("***************************\n");
			printf("* please inpurt exemple	:*\n");
			printf("* insert <name> <pass>	  *\n");
			printf("* delete <name> <pass>	  *\n");
			printf("* update <name> <passold> <passnew>*\n");
			printf("* select <name> <pass>	  *\n");
			printf("* show	 *\n");
			printf("***************************\n");
		}
		else*/
		if(0 == strcmp("show", buf) )
		{
			database_show(&db);
		}
		else
		if(0 == strncmp("insert", buf,5) )
		{
			char name[20] = {0};
			char pass[20] = {0};
			sscanf(buf+6, "%s%s", name, pass);
			*a=database_insert(&db, name, pass);
		}
		else
			//printf("cmd err ! try help !!!\n");
	   /* if(0 == strncmp("delete", buf, 6) )
		{
			char name[20] = {0};
			char pass[20] = {0};
			sscanf(buf+7, "%s%s", name, pass);
			database_delete(&db, name, pass);
		}
		else
			//printf("cmd err ! try help !!!\n");
	    if(0 == strncmp("update", buf, 6) )
		{
			char name[20] = {0};
			char passold[20] = {0};
			char passnew[20]={0};
			sscanf(buf+7, "%s%s%s", name, passold,passnew);
			database_update(&db, name, passold,passnew);

		}
		else
		if(0 == strcmp("save", buf) )
		{
			database_save(&db, "my.db");
		}
		else
			//printf("cmd err ! try help !!!\n");*/
	      if(0 == strncmp("select", buf, 6) )
		{
			char name[20] = {0};
			char pass[20] = {0};
			sscanf(buf+7, "%s%s", name, pass);
			*b=database_select(&db, name, pass);
		}
		else
			printf("cmd err ! try help !!!\n");

}
